package com.xyz.musicplayer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import beans.User;
import utils.DBAdapter;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    //声明控件
    EditText editTextForEmail,editTextForPasswd;
    Button buttonCancel,buttonLogin;
    TextView tvForRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        init();

        //操作栏添加导航按钮
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch ( item.getItemId() ){
            case android.R.id.home:{
                finish();
            }break;
            default:
                Toast.makeText(this, "通知管理员维护", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    private void init(){
        //控件实例化
        editTextForEmail = (EditText) findViewById(R.id.EditText_email);
        editTextForPasswd = (EditText) findViewById(R.id.EditText_passwd);
        buttonCancel = (Button) findViewById(R.id.button_cancel);
        buttonLogin = (Button) findViewById(R.id.button_login);
        tvForRegister = (TextView) findViewById(R.id.tv_register);

        //注册监听器
        tvForRegister.setOnClickListener(this);
        buttonCancel.setOnClickListener(this);
        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //数据库链接开启
                DBAdapter dbAdapter = new DBAdapter(LoginActivity.this);
                dbAdapter.open();

                //获取用户输入值
                String account = editTextForEmail.getText().toString();
                String passwd = editTextForPasswd.getText().toString();

                //查询数据库
                User loginUser = dbAdapter.queryOneUser(account);
                if ( loginUser == null )
                    //数据库没有该账号
                    Toast.makeText(LoginActivity.this, "该账户不存在或已注销", Toast.LENGTH_SHORT).show();
                else {
                    if ( passwd.equals("") ){
                        //密码栏为空
                        Toast.makeText(LoginActivity.this, "密码栏不能为空", Toast.LENGTH_SHORT).show();
                    }else if ( !passwd.equals(loginUser.getPasswd()) ){
                        //密码与数据库中的密码不一致
                        Toast.makeText(LoginActivity.this, "密码输入不正确", Toast.LENGTH_SHORT).show();
                    }else if ( passwd.equals(loginUser.getPasswd()) ){
                        //登录成功
                        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                        //返回登录成功的账户信息并封装在类中
                        intent.putExtra("aUser",loginUser);
                        startActivity(intent);
                    }else
                        //其他的逻辑问题
                        Toast.makeText(LoginActivity.this, "出现了一些问题", Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch ( v.getId() ){
            case R.id.button_cancel:{
                finish();
            }break;
            case R.id.tv_register:{
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                intent.setPackage("com.xyz.musicplayer");
                startActivity(intent);
            }break;
            default:
                break;
        }
    }


}